﻿Imports System
Imports System.Xml
Imports System.Text
Imports System.IO
Imports System.Collections.Generic
Imports Microsoft.VisualStudio.TestTools.UnitTesting
Imports Kirishima16.Net.Twitter.Scraping
Imports System.Diagnostics
Imports System.Runtime.Serialization
Imports Kirishima16.Net.Twitter

<TestClass()> _
Public Class TwitterClientTest
    Private _testContextInstance As TestContext

    '''<summary>
    '''現在のテストの実行についての情報および機能を
    '''提供するテスト コンテキストを取得または設定します。
    '''</summary>
    Public Property TestContext() As TestContext
        Get
            Return _testContextInstance
        End Get
        Set(ByVal value As TestContext)
            _testContextInstance = value
        End Set
    End Property

#Region "追加のテスト属性"
    '
    ' テストを作成する際には、次の追加属性を使用できます:
    '
    ' クラス内で最初のテストを実行する前に、ClassInitialize を使用してコードを実行してください
    '<ClassInitialize()> Public Shared Sub MyClassInitialize(ByVal testContext As TestContext)
    'End Sub
    '
    ' クラス内のテストをすべて実行したら、ClassCleanup を使用してコードを実行してください
    ' <ClassCleanup()> Public Shared Sub MyClassCleanup()
    ' End Sub
    '
    ' 各テストを実行する前に、TestInitialize を使用してコードを実行してください
    ' <TestInitialize()> Public Sub MyTestInitialize()
    ' End Sub
    '
    ' 各テストを実行した後に、TestCleanup を使用してコードを実行してください
    ' <TestCleanup()> Public Sub MyTestCleanup()
    ' End Sub
    '
#End Region

    Private _tw As New TwitterClient()

    Private Sub WriteObjectAsXml(ByVal obj As Object)
        Dim xs As New Serialization.XmlSerializer(obj.GetType())
        Using ms As New MemoryStream()
            xs.Serialize(ms, obj)
            ms.Flush()
            TestContext.WriteLine(Encoding.UTF8.GetString(ms.ToArray()))
        End Using
    End Sub

    <TestMethod()> _
    Public Sub LoginTest()
        Dim ret As LoginResult = _tw.Login(My.Settings.TwitterUserName, My.Settings.TwitterPassword)
        Assert.AreEqual(Of LoginResult)(LoginResult.Success, ret)
    End Sub

    <TestMethod()> _
    Public Sub LogoutTest()
        LoginTest()
        LogoutTestImpl()
    End Sub

    Public Sub LogoutTestImpl()
        Dim ret As LogoutResult = _tw.Logout()
        Assert.AreEqual(Of LogoutResult)(LogoutResult.Success, ret)
    End Sub

    <TestMethod()> _
    Public Sub GetFriendsTest()
        LoginTest()
        GetFriendsTestImpl()
        LogoutTestImpl()
    End Sub

    Private Sub GetFriendsTestImpl()
        Dim friends As IList(Of TwitterUser) = _tw.GetFriends()
        Assert.IsNotNull(friends)
    End Sub

    <TestMethod()> _
    Public Sub GetFirstPageFriendsTest()
        LoginTest()
        GetFirstPageFriendsTestImpl()
        LogoutTestImpl()
    End Sub

    Private Sub GetFirstPageFriendsTestImpl()
        Dim friends As IList(Of TwitterUser) = _tw.GetFriends(1, 1)
        Assert.IsNotNull(friends)
        Assert.AreEqual(Of Integer)(20, friends.Count)
#If DEBUG Then
        WriteObjectAsXml(friends.ToArray())
#End If
    End Sub

    <TestMethod()> _
    Public Sub GetFollowersTest()
        LoginTest()
        GetFollowersTestImpl()
        LogoutTestImpl()
    End Sub

    Public Sub GetFollowersTestImpl()
        Dim followers As IList(Of TwitterUser) = _tw.GetFollowers()
        Assert.IsNotNull(followers)
    End Sub

    <TestMethod()> _
    Public Sub GetFirstPageFollowersTest()
        LoginTest()
        GetFirstPageFollowersTestImpl()
        LogoutTestImpl()
    End Sub

    Private Sub GetFirstPageFollowersTestImpl()
        Dim followers As IList(Of TwitterUser) = _tw.GetFollowers(1, 1)
        Assert.IsNotNull(followers)
        Assert.AreEqual(Of Integer)(20, followers.Count)
#If DEBUG Then
        WriteObjectAsXml(followers.ToArray())
#End If
    End Sub

    <TestMethod()> _
    Public Sub CreateFriendShipTest()
        LoginTest()
        CreateFriendShipTestImpl()
        LogoutTestImpl()
    End Sub

    Private Sub CreateFriendShipTestImpl()
        Dim ret As Boolean = _tw.CreateFriendship(My.Settings.TwitterFriendShipUserName)
        Assert.IsTrue(ret)
    End Sub

    <TestMethod()> _
    Public Sub CreateFriendShipByUserInfoTest()
        LoginTest()
        CreateFriendShipByUserInfoTestImpl()
        LogoutTestImpl()
    End Sub

    Private Sub CreateFriendShipByUserInfoTestImpl()
        Dim userInfo As New TwitterUser()
        userInfo.UserID = My.Settings.TwitterFriendShipUserID
        userInfo.UserName = My.Settings.TwitterFriendShipUserName
        Dim ret As Boolean = _tw.CreateFriendship(userInfo)
        Assert.IsTrue(ret)
    End Sub

    <TestMethod()> _
    Public Sub DestroyFriendShipTest()
        LoginTest()
        DestroyFriendShipTestImpl()
        LogoutTestImpl()
    End Sub

    Private Sub DestroyFriendShipTestImpl()
        Dim ret As Boolean = _tw.DestroyFriendship(My.Settings.TwitterFriendShipUserName)
        Assert.IsTrue(ret)
    End Sub

    <TestMethod()> _
    Public Sub DestroyFriendShipByUserInfoTest()
        LoginTest()
        DestroyFriendShipByUserInfoTestImpl()
        LogoutTestImpl()
    End Sub

    Private Sub DestroyFriendShipByUserInfoTestImpl()
        Dim userInfo As New TwitterUser()
        userInfo.UserID = My.Settings.TwitterFriendShipUserID
        userInfo.UserName = My.Settings.TwitterFriendShipUserName
        Dim ret As Boolean = _tw.DestroyFriendship(userInfo)
        Assert.IsTrue(ret)
    End Sub

    <TestMethod()> _
    Public Sub CreateBlockTest()
        LoginTest()
        CreateBlockTestImpl()
        LogoutTestImpl()
    End Sub

    Private Sub CreateBlockTestImpl()
        Dim ret As Boolean = _tw.CreateBlock(My.Settings.TwitterBlockUserName)
        Assert.IsTrue(ret)
    End Sub

    <TestMethod()> _
    Public Sub CreateBlockByUserInfoTest()
        LoginTest()
        Dim userInfo As New TwitterUser()
        userInfo.UserID = My.Settings.TwitterBlockUserID
        userInfo.UserName = My.Settings.TwitterBlockUserName
        Dim ret As Boolean = _tw.CreateBlock(userInfo)
        Assert.IsTrue(ret)
        LogoutTestImpl()
    End Sub

    <TestMethod()> _
    Public Sub DestroyBlockTest()
        LoginTest()
        DestroyBlockTestImpl()
        LogoutTestImpl()
    End Sub

    Private Sub DestroyBlockTestImpl()
        Dim ret As Boolean = _tw.DestroyBlock(My.Settings.TwitterBlockUserName)
        Assert.IsTrue(ret)
    End Sub

    <TestMethod()> _
    Public Sub DestroyBlockByUserInfoTest()
        LoginTest()
        Dim userInfo As New TwitterUser()
        userInfo.UserID = My.Settings.TwitterBlockUserID
        userInfo.UserName = My.Settings.TwitterBlockUserName
        Dim ret As Boolean = _tw.DestroyBlock(userInfo)
        Assert.IsTrue(ret)
        LogoutTestImpl()
    End Sub

    <TestMethod()> _
    Public Sub UpdateStatusTest()
        LoginTest()
        UpdateStatusTestImpl()
        LogoutTestImpl()
    End Sub

    Private Sub UpdateStatusTestImpl()
        Dim status As TwitterStatus = _tw.UpdateStatus(My.Settings.TwitterPostStatus)
        Assert.IsNotNull(status)
#If DEBUG Then
        WriteObjectAsXml(status)
#End If
    End Sub

    <TestMethod()> _
Public Sub UpdateReplyStatusTest()
        LoginTest()
        UpdateReplyStatusTestImpl()
        LogoutTestImpl()
    End Sub

    Private Sub UpdateReplyStatusTestImpl()
        Dim status As TwitterStatus = _tw.UpdateStatus(String.Format("@{0} {1}", My.Settings.TwitterReplyUserName, My.Settings.TwitterReplyStatus), My.Settings.TwitterReplyStatusID)
        Assert.IsNotNull(status)
#If DEBUG Then
        WriteObjectAsXml(status)
#End If
    End Sub

    <TestMethod()> _
    Public Sub DestroyStatusTest()
        LoginTest()
        DestroyStatusTestImpl()
        LogoutTestImpl()
    End Sub

    Private Sub DestroyStatusTestImpl()
        Dim ret As Boolean = _tw.DestroyStatus(My.Settings.TwitterDestroyStatusID)
        Assert.IsTrue(ret)
    End Sub

    <TestMethod()> _
    Public Sub CreateFavoriteTest()
        LoginTest()
        CreateFavoriteTestImpl()
        LogoutTestImpl()
    End Sub

    Private Sub CreateFavoriteTestImpl()
        Dim ret As Boolean = _tw.CreateFavorite(My.Settings.TwitterFavoriteStatusID)
        Assert.IsTrue(ret)
    End Sub

    <TestMethod()> _
    Public Sub DestroyFavoriteTest()
        LoginTest()
        DestroyFavoriteTestImpl()
        LogoutTestImpl()
    End Sub

    Private Sub DestroyFavoriteTestImpl()
        Dim ret As Boolean = _tw.DestroyFavorite(My.Settings.TwitterFavoriteStatusID)
        Assert.IsTrue(ret)
    End Sub

    <TestMethod()> _
    Public Sub GetPublicTimelineTest()
        LoginTest()
        GetPublicTimelineTestImpl()
        LogoutTestImpl()
    End Sub

    Private Sub GetPublicTimelineTestImpl()
        Dim timeline As IList(Of TwitterStatus) = _tw.GetPublicTimeline(2, 0)
        Assert.IsNotNull(timeline)
        Assert.AreEqual(Of Integer)(40, timeline.Count)
#If DEBUG Then
        WriteObjectAsXml(timeline.Take(10).ToArray())
#End If
    End Sub

    <TestMethod()> _
    Public Sub GetHomeTimelineTest()
        LoginTest()
        GetHomeTimelineTestImpl()
        LogoutTestImpl()
    End Sub

    Private Sub GetHomeTimelineTestImpl()
        Dim timeline As IList(Of TwitterStatus) = _tw.GetHomeTimeline(2, 0)
        Assert.IsNotNull(timeline)
        Assert.AreEqual(Of Integer)(40, timeline.Count)
#If DEBUG Then
        WriteObjectAsXml(timeline.Take(10).ToArray())
#End If
    End Sub

    <TestMethod()> _
    Public Sub GetReplyTimelineTest()
        LoginTest()
        GetReplyTimelineTestImpl()
        LogoutTestImpl()
    End Sub

    Private Sub GetReplyTimelineTestImpl()
        Dim timeline As IList(Of TwitterStatus) = _tw.GetRepliesTimeline(2, 0)
        Assert.IsNotNull(timeline)
        Assert.AreNotEqual(Of Integer)(0, timeline.Count)
#If DEBUG Then
        WriteObjectAsXml(timeline.Take(10).ToArray())
#End If
    End Sub

    <TestMethod()> _
    Public Sub GetInboxTimelineTest()
        LoginTest()
        GetInboxTimelineTestImpl()
        LogoutTestImpl()
    End Sub

    Private Sub GetInboxTimelineTestImpl()
        Dim timeline As IList(Of TwitterStatus) = _tw.GetInboxTimeline(2, 0)
        Assert.IsNotNull(timeline)
        Assert.AreNotEqual(Of Integer)(0, timeline.Count)
#If DEBUG Then
        WriteObjectAsXml(timeline.Take(10).ToArray())
#End If
    End Sub

    <TestMethod()> _
    Public Sub GetSentTimelineTest()
        LoginTest()
        GetSentTimelineTestImpl()
        LogoutTestImpl()
    End Sub

    Private Sub GetSentTimelineTestImpl()
        Dim timeline As IList(Of TwitterStatus) = _tw.GetSentTimeline(2, 0)
        Assert.IsNotNull(timeline)
        Assert.AreNotEqual(Of Integer)(0, timeline.Count)
#If DEBUG Then
        WriteObjectAsXml(timeline.Take(10).ToArray())
#End If
    End Sub

    <TestMethod()> _
    Public Sub GetUserTimelineTest()
        LoginTest()
        GetUserTimelineTestImpl()
        LogoutTestImpl()
    End Sub

    Private Sub GetUserTimelineTestImpl()
        Dim timeline As IList(Of TwitterStatus) = _tw.GetUserTimeline(2, 0, My.Settings.TwitterTimelineUserName)
        Assert.IsNotNull(timeline)
        Assert.AreEqual(Of Integer)(40, timeline.Count)
#If DEBUG Then
        WriteObjectAsXml(timeline.Take(10).ToArray())
#End If
    End Sub

    <TestMethod()> _
Public Sub GetUserFavoritesTest()
        LoginTest()
        GetUserFavoritesTestTestImpl()
        LogoutTestImpl()
    End Sub

    Private Sub GetUserFavoritesTestTestImpl()
        Dim timeline As IList(Of TwitterStatus) = _tw.GetUserFavorites(2, 0, My.Settings.TwitterTimelineUserName)
        Assert.IsNotNull(timeline)
#If DEBUG Then
        WriteObjectAsXml(timeline.Take(10).ToArray())
#End If
    End Sub
End Class
